package com.jtjrenren.android.taxi.driver.core.user.forgetpwd;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.jtjrenren.android.taxi.driver.bean.api.CommonResult;
import com.jtjrenren.android.taxi.driver.R;

import com.jtjrenren.android.taxi.driver.base.BaseActivity;
import com.jtjrenren.android.taxi.driver.manger.api.ApiWorks;
import com.library.weiget.ClearEditText;
import com.library.utils.RegexUtils;
import com.library.utils.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/4/27.
 */
public class UserForgetPwdActivity extends BaseActivity {
    @BindView(R.id.forgetpwd_step1)
    LinearLayout ll_step1;
    @BindView(R.id.forgetpwd_step2)
    LinearLayout ll_step2;
    @BindView(R.id.forgetpwd_phone)
    ClearEditText et_phone;
    @BindView(R.id.forgetpwd_yzm)
    ClearEditText et_yzm;
    @BindView(R.id.forgetpwd_newpwd)
    ClearEditText et_pwdn;
    @BindView(R.id.forgetpwd_newpwd2)
    ClearEditText et_pwdqr;
    @BindView(R.id.forgetpwd_getyzm)
    Button bt_getyzm;

    String phone;

    private CountDownTimer countDownTimer = new CountDownTimer(60 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            bt_getyzm.setEnabled(false);
            bt_getyzm.setText(millisUntilFinished / 1000 + getString(R.string.miao));
        }

        @Override
        public void onFinish() {
            bt_getyzm.setEnabled(true);
            bt_getyzm.setText(getString(R.string.huoquyanzhengma));
        }
    };

    @Override
    public int getLayoutID() {
        return R.layout.aty_user_forgetpwd;
    }

    @Override
    public void init() {
        initHandler();
        ((TextView) findViewById(R.id.title_title)).setText("忘记密码");
        findViewById(R.id.title_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }


    @OnClick(R.id.forgetpwd_getyzm)
    public void clickYzm() {
        phone = et_phone.getText().toString().trim();
        if (StringUtils.isEmpty(phone) || !RegexUtils.isPhoneNo(phone)) {
            displayShort(getString(R.string.qingshuruzhengquedeshoujihaoma));
            return;
        }
        checkPhoneUni(phone);
    }

    @OnClick(R.id.forgetpwd_next)
    public void clickNext() {
        if (StringUtils.isEmpty(et_yzm.getText().toString().trim())) {
            displayShort(getString(R.string.qingshuruzhengquedeyanzhengma));
            return;
        }
        checkYzm();
    }

    @OnClick(R.id.forgetpwd_qrxg)
    public void clickQrxg() {
        String pwd = et_pwdn.getText().toString().trim();
        String pwdn = et_pwdqr.getText().toString().trim();
        if (!pwd.equals(pwdn)) {
            displayShort(getString(R.string.qingshuruzhegnquedemima));
            return;
        }

        resetPwd(pwd);
    }


    private void checkPhoneUni(final String phone) {
        showProDialog(null, getString(R.string.jianceshoujihaoshifouzhuce));
        ApiWorks.isUniquePhone(phone, new ApiWorks.ResponseListener<CommonResult>() {
            @Override
            public void onResponse(CommonResult commonResult) {
                disProDialog();
                if (commonResult != null) {
                    if (commonResult.getCode() == 1) {
                        ApiWorks.sendAuthCode(phone, "0");
                        countDownTimer.start();
                    } else
                        displayShort(commonResult.getMsg());
                }
            }
        });
    }


    private void checkYzm() {
        showProDialog(null, getString(R.string.qiaoyanyanzhengma));
        String phone = et_phone.getText().toString().trim();
        String yzm = et_yzm.getText().toString().trim();
        ApiWorks.checkYzm(phone, yzm, "0", new ApiWorks.ResponseListener<CommonResult>() {
            @Override
            public void onResponse(CommonResult commonResult) {
                disProDialog();
                if (commonResult != null && commonResult.getCode() == 1) {
                    ll_step1.setVisibility(View.GONE);
                    ll_step2.setVisibility(View.VISIBLE);
                } else
                    displayShort(getString(R.string.yanzhegnmajiaoyanshibai));
            }
        });
    }

    private void resetPwd(String pwd) {
        ApiWorks.resetPwd(pwd, phone, new ApiWorks.ResponseListener<CommonResult>() {
            @Override
            public void onResponse(CommonResult commonResult) {
                if (commonResult != null && commonResult.getCode() == 1) {
                    displayShort(getString(R.string.mimachongzhichenggong));

                    setResult(RESULT_OK);
                    finish();
                } else {
                    displayShort("密码重置失败 ");
                }
            }
        });
    }

}
